package net.orange.framework.controller;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import net.orange.architecture.db.ListConditions;
import net.orange.architecture.process.ProcessFactory;
import net.orange.architecture.web.BaseController;
import net.orange.framework.logic.IDepartmentProcess;
import net.orange.framework.logic.IGroupProcess;
import net.orange.framework.model.Department;
import net.orange.framework.model.Group;
import net.orange.framework.model.User;

/**
 * The user managed bean using in JSF MVC modal. it is include the web page
 * content and control the action and page forward direction in the web site.
 */
@ManagedBean(name="user")
@SessionScoped
public class UserController extends BaseController {

	/**
	 * The user code want to search.
	 */
	private String sr_user_code;

	/**
	 * The user name want to search
	 */
	private String sr_user_name;

	/**
	 * The login account want to search.
	 */
	private String sr_login_no;

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.orange.architecture.web.BaseController#getClazz()
	 */
	protected Class getClazz() {
		return User.class;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.orange.architecture.web.BaseController#loadConditions(net.orange.architecture.db.ListConditions)
	 */
	public void loadConditions(ListConditions lc) throws Exception {
		super.loadConditions(lc);
	}

	/**
	 * @return Returns the sr_login_no.
	 */
	public String getSr_login_no() {
		return sr_login_no;
	}

	/**
	 * @return Returns the sr_user_code.
	 */
	public String getSr_user_code() {
		return sr_user_code;
	}

	/**
	 * @return Returns the sr_user_name.
	 */
	public String getSr_user_name() {
		return sr_user_name;
	}

	/**
	 * @param sr_login_no
	 *            The sr_login_no to set.
	 */
	public void setSr_login_no(String sr_login_no) {
		this.sr_login_no = sr_login_no;
	}

	/**
	 * @param sr_user_code
	 *            The sr_user_code to set.
	 */
	public void setSr_user_code(String sr_user_code) {
		this.sr_user_code = sr_user_code;
	}

	/**
	 * @param sr_user_name
	 *            The sr_user_name to set.
	 */
	public void setSr_user_name(String sr_user_name) {
		this.sr_user_name = sr_user_name;
	}

	/**
	 * @return the dept_code
	 */
	public String getDeparment_code() {
		User user = (User) content;
		return (user != null && user.getDepartment() != null) ? user
				.getDepartment().getDept_code() : null;
	}

	/**
	 * @param dept_code
	 *            the dept_code to set
	 */
	public void setDeparment_code(String dept_code) throws Exception {
		User user = (User) content;
		IDepartmentProcess process = (IDepartmentProcess) ProcessFactory
				.get(Department.class);

		if (user != null) {
			user.setDepartment((Department) process
					.find("dept_code", dept_code));
		}
	}

	/**
	 * @return Returns the group_pks.
	 */
	public String[] getGroup_pks() {
		User user = (User) content;

		if (user == null || user.getGroups() == null
				|| user.getGroups().size() <= 0)
			return null;

		Iterator groups = user.getGroups().iterator();
		String[] pks = new String[user.getGroups().size()];

		for (int i = 0; groups.hasNext(); ++i) {
			Group group = (Group) groups.next();
			pks[i] = group.getPk() + "";
		}

		return pks;
	}

	/**
	 * @param group_pks
	 *            The group_pks to set.
	 */
	public void setGroup_pks(String[] group_pks) throws Exception {
		User user = (User) content;
		if (user != null && group_pks != null && group_pks.length > 0) {
			IGroupProcess gprocess = (IGroupProcess) ProcessFactory
					.get(Group.class);
			Set<Group>  groups = new HashSet<Group>();
			user.getGroups().clear();

			for (int i = 0; i < group_pks.length; i++) {
				String pk = group_pks[i];

				if (pk != null && pk.length() > 0) {
					Group group = (Group) gprocess.find(Long.parseLong(pk));

					if (group != null)
						groups.add(group);
				}
			}
			user.setGroups(groups);
		}
	}
	

}
